#!/bin/sh

if [ -z "$(uci -q get v2ray.main)" ]; then
	touch /etc/config/v2ray
	uci batch <<-EOF
		set v2ray.main=v2ray
		set v2ray.main.v2ray_file='/usr/bin/v2ray'
		set v2ray.main.mem_percentage='0'
		set v2ray.main.loglevel='debug'
		set v2ray.main.access_log='/dev/null'
		set v2ray.main.error_log='/dev/null'
		set v2ray.main.enabled='0'
		set v2ray.main.outbounds='omrout'
		set v2ray.main.inbounds='omr'
		add_list v2ray.main.inbounds='omrtest'
		set v2ray.main_dns=dns
		set v2ray.main_dns.hosts='example.com|127.0.0.1'
		set v2ray.main_dns.enabled='0'
		set v2ray.main_policy=policy
		set v2ray.main_policy.enabled='1'
		set v2ray.main_policy.levels='policy_level_0'
		set v2ray.policy_level_0=policy_level
		set v2ray.policy_level_0.level='0'
		set v2ray.policy_level_0.handshake='4'
		set v2ray.policy_level_0.conn_idle='2400'
		set v2ray.policy_level_0.uplink_only='0'
		set v2ray.policy_level_0.downlink_only='0'
		set v2ray.policy_level_0.buffer_size='512'
		set v2ray.main_transparent_proxy=transparent_proxy
		set v2ray.main_transparent_proxy.proxy_mode='default'
		set v2ray.main_transparent_proxy.apnic_delegated_mirror='apnic'
		set v2ray.main_transparent_proxy.gfwlist_mirror='github'
		set v2ray.main_transparent_proxy.redirect_udp='1'
		set v2ray.main_transparent_proxy.redirect_port='1897'
		set v2ray.omrout=outbound
		set v2ray.omrout.tag='omrout_tunnel'
		set v2ray.omrout.protocol='vless'
		set v2ray.omrout.s_vmess_address=''
		set v2ray.omrout.s_vmess_port='65228'
		set v2ray.omrout.s_vmess_user_id=''
		set v2ray.omrout.s_vmess_user_security='none'
		set v2ray.omrout.s_vmess_user_alter_id='0'
		set v2ray.omrout.s_vless_address=''
		set v2ray.omrout.s_vless_port='65228'
		set v2ray.omrout.s_vless_user_id=''
		set v2ray.omrout.s_vless_user_security='none'
		set v2ray.omrout.s_vless_user_encryption='none'
		set v2ray.omrout.s_vless_user_alter_id='0'
		set v2ray.omrout.ss_network='tcp'
		set v2ray.omrout.ss_security='tls'
		set v2ray.omrout.ss_tls_allow_insecure='1'
		set v2ray.omrout.ss_tls_disable_system_root='1'
		set v2ray.omrout.ss_tls_cert_usage='verify'
		set v2ray.omrout.ss_tls_cert_file='/etc/luci-uploads/client.crt'
		set v2ray.omrout.ss_tls_key_file='/etc/luci-uploads/client.key'
		set v2ray.omrout.mux_concurrency='8'
		set v2ray.omr=inbound
		set v2ray.omr.tag='omrtunnel'
		set v2ray.omr.listen='0.0.0.0'
		set v2ray.omr.port='1897'
		set v2ray.omr.protocol='dokodemo-door'
		set v2ray.omr.s_dokodemo_door_network='tcp'
		add_list v2ray.omr.s_dokodemo_door_network='udp'
		set v2ray.omr.ss_sockopt_tproxy='redirect'
		set v2ray.omr.ss_sockopt_tcp_fast_open='1'
		set v2ray.omr.s_dokodemo_door_follow_redirect='1'
		set v2ray.omr6=inbound
		set v2ray.omr6.tag='omrtunnel6'
		set v2ray.omr6.listen='::'
		set v2ray.omr6.port='1898'
		set v2ray.omr6.protocol='dokodemo-door'
		set v2ray.omr6.s_dokodemo_door_network='tcp'
		add_list v2ray.omr6.s_dokodemo_door_network='udp'
		set v2ray.omr6.ss_sockopt_tproxy='tproxy'
		set v2ray.omr6.ss_sockopt_tcp_fast_open='1'
		set v2ray.omr6.s_dokodemo_door_follow_redirect='1'
		set v2ray.omrtest=inbound
		set v2ray.omrtest.port='1111'
		set v2ray.omrtest.protocol='socks'
		set v2ray.omrtest.listen='127.0.0.1'
		set v2ray.omrtest.s_socks_auth='noauth'
		set v2ray.omrtest.s_socks_udp='1'
		set v2ray.omrtest.s_socks_ip='127.0.0.1'
		set v2ray.omrtest.s_socks_userlevel='0'
		commit v2ray
	EOF
fi
uci -q batch <<-EOF >/dev/null
	set v2ray.omr.listen='0.0.0.0'
	commit v2ray
EOF

if [ "$(uci -q get firewall.v2ray)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set firewall.v2ray=include
		set firewall.v2ray.path=/etc/firewall.v2ray-rules
		set firewall.v2ray.reload=1
		commit firewall
	EOF
fi
if [ "$(uci -q get firewall.v2ray.path)" != "/etc/firewall.v2ray-rules" ]; then
	uci -q batch <<-EOF >/dev/null
		set firewall.v2ray.path=/etc/firewall.v2ray-rules
		commit firewall
	EOF
fi

if [ "$(uci -q get v2ray.main_reverse.bridges | grep omrbridge)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set v2ray.main_reverse=reverse
		set v2ray.main_reverse.enabled=1
		set v2ray.main_reverse.bridges='omrbridge|omr.lan'
		commit v2ray
	EOF
fi
if [ "$(uci -q get v2ray.omrrouting)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set v2ray.omrexit=outbound
		set v2ray.omrexit.protocol='freedom'
		set v2ray.omrexit.tag='out'
		add_list v2ray.main.outbounds=omrexit
		set v2ray.omrrouting=routing_rule
		set v2ray.omrrouting.type='field'
		set v2ray.omrrouting.inbound_tag='omrbridge'
		set v2ray.omrrouting.outbound_tag='omrout_tunnel'
		set v2ray.omrrouting.domain='full:omr.lan'
		set v2ray.omrroutingo=routing_rule
		set v2ray.omrroutingo.type='field'
		set v2ray.omrroutingo.inbound_tag='omrbridge'
		set v2ray.omrroutingo.outbound_tag='out'
		set v2ray.main_routing=routing
		set v2ray.main_routing.enabled=1
		set v2ray.main_routing.rules='omrrouting'
		add_list v2ray.main_routing.rules='omrroutingo'
		commit v2ray
	EOF
fi

if [ "$(uci -q get v2ray.main.error_log)" != "/dev/null" ]; then
	uci -q batch <<-EOF >/dev/null
		set v2ray.main.error_log='/dev/null'
		commit v2ray
	EOF
fi

exit 0